﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace wpNewUsers.wpNewUsers
{
    [ToolboxItemAttribute(false)]
    public class wpNewUsers : WebPart
    {
        int myCountDay = 5;

        [WebBrowsable(true), WebDisplayName("Кол-во дней"), WebDescription("Кол-во дней вперед."),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public int CountDay
        {
            get { return myCountDay; }
            set { myCountDay = value; }
        }

        [WebBrowsable(true),
        Category("(Ko) Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Сотрудники магазинов: ")]
        public bool CustomCheckboxProp { get; set; } 

        DateTime dDate1 = DateTime.Now.AddDays(-1);
        public List<string> mMonthName = new List<string>() { "Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августа", "Сентября", "	Октября", "Ноября", "Декабря" };
        DateTime d2 ;

        string sURL = "http://sp.korablik.local";
        List<dsData> dsProperty = new List<dsData>();

        protected override void CreateChildControls()
        {
            //--
            d2 = DateTime.Now.AddDays(myCountDay);
            //--
            SPSite site = new SPSite(sURL);
            SPServiceContext context = SPServiceContext.GetContext(site);
            UserProfileManager uprofileManager = new UserProfileManager(context);
            foreach(UserProfile uPr in uprofileManager)
            {
                if (uPr["SPS-Birthday"].Value != null && (uPr["Office"].Value.ToString().Equals("Румянцево") != CustomCheckboxProp))
                {
                    DateTime d3 = DateTime.Parse(uPr["SPS-Birthday"].Value.ToString());
                    int iYear = d3.Year;
                    iYear = dDate1.Year - iYear;
                    d3 = d3.AddYears(iYear);
                    if (dDate1 < d3 && d2 > d3)
                    {
                        dsData dRow = new dsData();
                        dRow.Birthday = d3;
                        dRow.FullName = uPr["PreferredName"].Value.ToString();
                        if (uPr["SPS-JobTitle"].Value!=null)
                            dRow.Staff = uPr["SPS-JobTitle"].Value.ToString();
                        if(uPr["Department"].Value!=null)
                            dRow.Department = uPr["Department"].Value.ToString();
                        if (uPr["Office"].Value!= null)
                            dRow.Office = uPr["Office"].Value.ToString();
                        if (uPr["PictureUrl"].Value != null)
                            dRow.ImageUrl = uPr["PictureUrl"].Value.ToString();
                        else
                            dRow.ImageUrl = sURL + "/_layouts/IMAGES/no-photo.png";
                        if (uPr.PersonalSite != null)
                            dRow.SiteUrl = uPr.PersonalSite.Url.ToString();
                        //--
                        dsProperty.Add(dRow);
                    }
                }
            }
            //--
            dsProperty.Sort((x,y) => DateTime.Compare(x.Birthday,y.Birthday));  
            //--
            Table tb = new Table();
            TableRow tRow;
            TableCell tCell;
            foreach (dsData row in dsProperty)
            {
                //--
                tRow = new TableRow();
                tRow.BorderWidth = 0;
                //--
                tCell = new TableCell();
                tCell.Width = 90;

                Image im = new Image();
                im.BorderWidth = 1;
                im.Width = 80;
                im.ImageUrl = row.ImageUrl;

                tCell.Controls.Add(im);
                tRow.Cells.Add(tCell);
                //--
                tCell = new TableCell();
                tCell.Width = 200;
                tCell.VerticalAlign = VerticalAlign.Top;
                tCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
                //--
                Label l2 = new Label();
                l2.Text = row.Birthday.Day.ToString() + " " + mMonthName[row.Birthday.Month - 1] + "<br>";
                l2.Font.Size = 10;
                l2.Font.Bold = true;
                l2.ForeColor = System.Drawing.Color.Blue;
                tCell.Controls.Add(l2);
                //--
                if (row.SiteUrl != null)
                {
                    HyperLink hl1 = new HyperLink();
                    hl1.Text = row.FullName + "<br>";
                    hl1.Font.Size = 8;
                    hl1.NavigateUrl = row.SiteUrl;
                    tCell.Controls.Add(hl1);
                }
                else
                {
                    Label l1 = new Label();
                    l1.Text = row.FullName + "<br>";
                    l1.Font.Size = 8;
                    tCell.Controls.Add(l1);
                }
                //--
                if (row.Department != null)
                {
                    Label l4 = new Label();
                    l4.Text = row.Department + "<br>";
                    l4.Font.Size = 9;
                    tCell.Controls.Add(l4);
                }
                //--
                if (row.Staff != null)
                {
                    Label l5 = new Label();
                    l5.Text = row.Staff + "<br>";
                    l5.Font.Size = 9;
                    tCell.Controls.Add(l5);
                }
                //--
                if (row.Office != null)
                {
                    Label l3 = new Label();
                    l3.Text = row.Office;
                    l3.Font.Size = 9;
                    tCell.Controls.Add(l3);
                }
                //--
                tRow.Cells.Add(tCell);
                //--
                tb.Rows.Add(tRow);
            }
            //--
            this.Controls.Add(tb);
        }
    }
}
